package org.example.course.controller;

import org.example.course.common.ResultObj;
import org.example.course.common.utils.StringUtils;
import org.example.course.common.utils.WebUtils;
import org.example.course.domain.User;
import org.example.course.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Classname loginController
 * @Description 登录
 * @Date 2020/5/8 12:54
 * @Created by
 */
@Controller
public class loginController {
    @Autowired
    private UserService userService;

    /**
     * 登录方法
     *
     * @param loginName
     * @param password
     * @param vercode
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public ResultObj login(String loginName, String password, String vercode) {
        if (StringUtils.isOneOfBlankEmptyNULL(loginName) || StringUtils.isOneOfBlankEmptyNULL(password)) {
            return ResultObj.LOGIN_ERROR_PASS;
        }
        User user = userService.loginByNameAndPwd(loginName, password);
        if (user != null) {
            //保存用户到session
            WebUtils.setSessionUser(user);
            return ResultObj.LOGIN_SUCCESS;
        }
        return ResultObj.LOGIN_ERROR_PASS;
    }
}
